/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package times.persistence.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import times.persistence.entities.Category;
import times.persistence.entities.PriceSlaughter;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import times.persistence.controllers.exceptions.IllegalOrphanException;
import times.persistence.controllers.exceptions.NonexistentEntityException;
import times.persistence.entities.CategoryClass;

/**
 *
 * @author Administrator
 */
public class CategoryClassJpaController implements Serializable {

    public CategoryClassJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(CategoryClass categoryClass) {
        if (categoryClass.getPriceSlaughterList() == null) {
            categoryClass.setPriceSlaughterList(new ArrayList<PriceSlaughter>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Category categoryId = categoryClass.getCategoryId();
            if (categoryId != null) {
                categoryId = em.getReference(categoryId.getClass(), categoryId.getId());
                categoryClass.setCategoryId(categoryId);
            }
            List<PriceSlaughter> attachedPriceSlaughterList = new ArrayList<PriceSlaughter>();
            for (PriceSlaughter priceSlaughterListPriceSlaughterToAttach : categoryClass.getPriceSlaughterList()) {
                priceSlaughterListPriceSlaughterToAttach = em.getReference(priceSlaughterListPriceSlaughterToAttach.getClass(), priceSlaughterListPriceSlaughterToAttach.getId());
                attachedPriceSlaughterList.add(priceSlaughterListPriceSlaughterToAttach);
            }
            categoryClass.setPriceSlaughterList(attachedPriceSlaughterList);
            em.persist(categoryClass);
            if (categoryId != null) {
                categoryId.getCategoryClassList().add(categoryClass);
                categoryId = em.merge(categoryId);
            }
            for (PriceSlaughter priceSlaughterListPriceSlaughter : categoryClass.getPriceSlaughterList()) {
                CategoryClass oldCategoryClassIdOfPriceSlaughterListPriceSlaughter = priceSlaughterListPriceSlaughter.getCategoryClassId();
                priceSlaughterListPriceSlaughter.setCategoryClassId(categoryClass);
                priceSlaughterListPriceSlaughter = em.merge(priceSlaughterListPriceSlaughter);
                if (oldCategoryClassIdOfPriceSlaughterListPriceSlaughter != null) {
                    oldCategoryClassIdOfPriceSlaughterListPriceSlaughter.getPriceSlaughterList().remove(priceSlaughterListPriceSlaughter);
                    oldCategoryClassIdOfPriceSlaughterListPriceSlaughter = em.merge(oldCategoryClassIdOfPriceSlaughterListPriceSlaughter);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CategoryClass categoryClass) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CategoryClass persistentCategoryClass = em.find(CategoryClass.class, categoryClass.getId());
            Category categoryIdOld = persistentCategoryClass.getCategoryId();
            Category categoryIdNew = categoryClass.getCategoryId();
            List<PriceSlaughter> priceSlaughterListOld = persistentCategoryClass.getPriceSlaughterList();
            List<PriceSlaughter> priceSlaughterListNew = categoryClass.getPriceSlaughterList();
            List<String> illegalOrphanMessages = null;
            for (PriceSlaughter priceSlaughterListOldPriceSlaughter : priceSlaughterListOld) {
                if (!priceSlaughterListNew.contains(priceSlaughterListOldPriceSlaughter)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain PriceSlaughter " + priceSlaughterListOldPriceSlaughter + " since its categoryClassId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (categoryIdNew != null) {
                categoryIdNew = em.getReference(categoryIdNew.getClass(), categoryIdNew.getId());
                categoryClass.setCategoryId(categoryIdNew);
            }
            List<PriceSlaughter> attachedPriceSlaughterListNew = new ArrayList<PriceSlaughter>();
            for (PriceSlaughter priceSlaughterListNewPriceSlaughterToAttach : priceSlaughterListNew) {
                priceSlaughterListNewPriceSlaughterToAttach = em.getReference(priceSlaughterListNewPriceSlaughterToAttach.getClass(), priceSlaughterListNewPriceSlaughterToAttach.getId());
                attachedPriceSlaughterListNew.add(priceSlaughterListNewPriceSlaughterToAttach);
            }
            priceSlaughterListNew = attachedPriceSlaughterListNew;
            categoryClass.setPriceSlaughterList(priceSlaughterListNew);
            categoryClass = em.merge(categoryClass);
            if (categoryIdOld != null && !categoryIdOld.equals(categoryIdNew)) {
                categoryIdOld.getCategoryClassList().remove(categoryClass);
                categoryIdOld = em.merge(categoryIdOld);
            }
            if (categoryIdNew != null && !categoryIdNew.equals(categoryIdOld)) {
                categoryIdNew.getCategoryClassList().add(categoryClass);
                categoryIdNew = em.merge(categoryIdNew);
            }
            for (PriceSlaughter priceSlaughterListNewPriceSlaughter : priceSlaughterListNew) {
                if (!priceSlaughterListOld.contains(priceSlaughterListNewPriceSlaughter)) {
                    CategoryClass oldCategoryClassIdOfPriceSlaughterListNewPriceSlaughter = priceSlaughterListNewPriceSlaughter.getCategoryClassId();
                    priceSlaughterListNewPriceSlaughter.setCategoryClassId(categoryClass);
                    priceSlaughterListNewPriceSlaughter = em.merge(priceSlaughterListNewPriceSlaughter);
                    if (oldCategoryClassIdOfPriceSlaughterListNewPriceSlaughter != null && !oldCategoryClassIdOfPriceSlaughterListNewPriceSlaughter.equals(categoryClass)) {
                        oldCategoryClassIdOfPriceSlaughterListNewPriceSlaughter.getPriceSlaughterList().remove(priceSlaughterListNewPriceSlaughter);
                        oldCategoryClassIdOfPriceSlaughterListNewPriceSlaughter = em.merge(oldCategoryClassIdOfPriceSlaughterListNewPriceSlaughter);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = categoryClass.getId();
                if (findCategoryClass(id) == null) {
                    throw new NonexistentEntityException("The categoryClass with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CategoryClass categoryClass;
            try {
                categoryClass = em.getReference(CategoryClass.class, id);
                categoryClass.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The categoryClass with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<PriceSlaughter> priceSlaughterListOrphanCheck = categoryClass.getPriceSlaughterList();
            for (PriceSlaughter priceSlaughterListOrphanCheckPriceSlaughter : priceSlaughterListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This CategoryClass (" + categoryClass + ") cannot be destroyed since the PriceSlaughter " + priceSlaughterListOrphanCheckPriceSlaughter + " in its priceSlaughterList field has a non-nullable categoryClassId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Category categoryId = categoryClass.getCategoryId();
            if (categoryId != null) {
                categoryId.getCategoryClassList().remove(categoryClass);
                categoryId = em.merge(categoryId);
            }
            em.remove(categoryClass);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CategoryClass> findCategoryClassEntities() {
        return findCategoryClassEntities(true, -1, -1);
    }

    public List<CategoryClass> findCategoryClassEntities(int maxResults, int firstResult) {
        return findCategoryClassEntities(false, maxResults, firstResult);
    }

    private List<CategoryClass> findCategoryClassEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CategoryClass.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CategoryClass findCategoryClass(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CategoryClass.class, id);
        } finally {
            em.close();
        }
    }

    public int getCategoryClassCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CategoryClass> rt = cq.from(CategoryClass.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
